package com.gljx.web.service.impl;

import com.gljx.constant.ActivitiKey;
import com.gljx.util.*;
import com.gljx.web.Entity.Dropexercisethree;
import com.gljx.web.Entity.Dropexercisethreeentry;
import com.gljx.web.Entity.Student;
import com.gljx.web.Entity.User;
import com.gljx.web.dao.*;
import com.gljx.web.service.CustomLogService;
import com.gljx.web.service.DropexerciseThreeService;
import com.gljx.web.service.SharecarthreeregService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/24 0024.
 */
@Transactional
@Service
public class DropexerciseThreeServiceImpl extends CommonDao implements DropexerciseThreeService {

    @Autowired
    DropexercisethreeMapper dropexercisethreeMapper;
    @Autowired
    DropexercisethreeentryMapper dropexercisethreeentryMapper;
    @Autowired
    SharecarthreeregentryMapper sharecarthreeregentryMapperl;
    @Autowired
    SharecarthreeregService sharecarthreeregService;
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    CustomLogService customLogService;
    @Autowired
    HttpSession session;

    @Override
    public Map<String, Object> selectPage(int pindex, String psql, int psize) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Pindex", pindex);
        paramMap.put("Psql", psql);
        paramMap.put("Psize", psize);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
        this.getSqlSession().select("com.gljx.web.dao.DropexercisethreeMapper.pagemap", paramMap, null);
        return paramMap;
    }

    @Override
    public Map<String, Object> selectPageCount(String psql) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Psql", psql);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        this.getSqlSession().select("com.gljx.web.dao.DropexercisethreeMapper.pagecount", paramMap, null);
        return paramMap;
    }

    /**
     * @Author lcx
     * @Date 2017/11/28 0028 上午 9:03
     * 添加和更新数据
     */
    @Override
    public void adddropexercisethree(Dropexercisethree dropexercisethree, String[] cfstudentid,
                                     String[] cfstudentName, String[] cfstudentno, String[] cfidcardno,
                                     String[] cfcoachid, String[] cfcoachName, String[] cfcarpeoplenum,
                                     String[] cflcts, String[] cftlyy, String state) {

        //删除老的信息在次添加
        if ("1".equals(state)) {
            //查询之前的信息
            Dropexercisethree dropexercisethreetem = dropexercisethreeMapper.selectByPrimaryKey(dropexercisethree.getFid());
            dropexercisethree.setFcreatorid(dropexercisethreetem.getFcreatorid());
            dropexercisethree.setFcreatetime(dropexercisethreetem.getFcreatetime());
            dropexercisethreeMapper.deleteByPrimaryKey(dropexercisethree.getFid());
            dropexercisethreeentryMapper.deleteByFparentid(dropexercisethree.getFid());
            //为了消除提交页面点击确定丢失审核人的 问题
            if ("2".equals(dropexercisethree.getFfivouchered() + "")) {
                dropexercisethree.setFauditorid(dropexercisethreetem.getFauditorid());
                dropexercisethree.setCfbillstate(Long.valueOf("3"));
            }
            if ("4".equals(dropexercisethree.getFfivouchered() + "")) {
                Axamine axamine = new Axamine();
                axamine.submissionProcess(String.valueOf(dropexercisethree.getFnumber()));
            }

        }

        /*获取uuid*/
        String uuid = GetFid.UUIDIntoFid();
        dropexercisethree.setFid(uuid);

        for (int i = 0; i < cfstudentid.length; i++) {
            Dropexercisethreeentry dropexercisethreeentry = new Dropexercisethreeentry();
            dropexercisethreeentry.setFid(GetFid.UUIDIntoFid());
            dropexercisethreeentry.setFparentid(uuid);
            dropexercisethreeentry.setCfstudentid(cfstudentid[i]);
            dropexercisethreeentry.setCfstuno(cfstudentno[i]);//学员编码
            dropexercisethreeentry.setCfstucardno(cfidcardno[i]);//身份证号码
            dropexercisethreeentry.setCfcoachid(cfcoachid[i]);
            dropexercisethreeentry.setCfcarpeoplenum(Long.valueOf(cfcarpeoplenum[i].equals("") ? "0" : cfcarpeoplenum[i]));
            BigDecimal cflcts1 = new BigDecimal(cflcts[i].equals("") ? "0" : cflcts[i]);
            dropexercisethreeentry.setCflcts(cflcts1);
            dropexercisethreeentry.setCftlyy(cftlyy[i]);

            dropexercisethreeentryMapper.insertSelective(dropexercisethreeentry);

            if ("1".equals(state)) {
                //添加学员服务日志
                customLogService.addLog("科目三退练进行修改", "修改了学员:" +
                                dropexercisethreeentry.getCfstuno() + cfstudentName[i] + "科目三退练!退练教练:"
                                + cfcoachName[i] + "退练原因:" + cftlyy[i] + "单据编号：" + dropexercisethree.getFnumber(),
                        dropexercisethreeentry.getCfstudentid(), ((User) session.getAttribute("user")).getFid());
            } else {
                //添加学员服务日志
                customLogService.addLog("科目三退练进行增加", "新增了学员:" +
                                dropexercisethreeentry.getCfstuno() + cfstudentName[i] + "科目三退练!退练教练:" + cfcoachName[i]
                                + "退练原因:" + cftlyy[i] + "单据编号：" + dropexercisethree.getFnumber(),
                        dropexercisethreeentry.getCfstudentid(), ((User) session.getAttribute("user")).getFid());
            }
        }
        dropexercisethreeMapper.insertSelective(dropexercisethree);
    }

    @Override
    public List<Dropexercisethreeentry> selectDropexercisethreeregAsXls(String fnumber, String fdescription, String cfstudentname, String beginTime, String endTime, String fcompanyid) {
        return dropexercisethreeMapper.selectDropexercisethreeregAsXls(fnumber, fdescription, cfstudentname, beginTime, endTime, fcompanyid);
    }

    //跳转到更新页面需要的信息
    @Override
    public Dropexercisethree selectDropexercisethreeByFid(String fid) {

        Dropexercisethree dropexercisethree = dropexercisethreeMapper.selectByPrimaryKey(fid);

        if (dropexercisethree != null) {
            dropexercisethree.setDropexercisethreeentryList(dropexercisethreeentryMapper.selectDropexercisethreeentryByFparentid(fid, ""));
        }

        return dropexercisethree;
    }

    /**
     * @Author lcx
     * @Date 2017/11/28 0028 上午 9:04
     * 删除
     */
    @Override
    public void deleteByDropexercisethreeKey(String[] deleteId, String[] fnumber) {
        ActitiviUtil actitiviUtil = new ActitiviUtil();

        for (int i = 0; i < deleteId.length; i++) {

            //添加学员服务日志
            List<Dropexercisethreeentry> dropexercisethreeentries = dropexercisethreeentryMapper.selectDropexercisethreeentyyByFparentidAsthree(deleteId[i]);

            for (Dropexercisethreeentry dropexercisethreeentry : dropexercisethreeentries) {
                customLogService.addLog("科目三退练进行删除", "删除了学员:" +
                                dropexercisethreeentry.getCfstuno() + dropexercisethreeentry.getCfstudentName() + "科目三退练!"
                                + "单据编号：" + dropexercisethreeentry.getFnumber(),
                        dropexercisethreeentry.getCfstudentid(), ((User) session.getAttribute("user")).getFid());
            }

            actitiviUtil.deleteTask(ActivitiKey.PUBLIC_KEY, fnumber[i]);
            dropexercisethreeMapper.deleteByPrimaryKey(deleteId[i]);
            dropexercisethreeentryMapper.deleteByFparentid(deleteId[i]);

        }
    }


    @Override
    public ServerResponse examine(Dropexercisethree dropexercisethree, String state, String agreed,
                                  String auditcomment, String cflcsjid, String[] studentid, String[] cftlyy) {
        //如果是审核
        Axamine axamine = new Axamine();
        if ("5".equalsIgnoreCase(state)) {
            ServerResponse serverResponse = axamine.auditProcess(dropexercisethree.getFnumber(), agreed, auditcomment);
            //如果返回 1 代表 流程结束 2代表流程还在继续 0 代表错误
            if (serverResponse.getStatus() == 1) {

                //修改学生的状态
                for (int i = 0; i < studentid.length; i++) {
                    Long studentState = sharecarthreeregService.selectStudentstateAsLcsjAndStudentId(cflcsjid, studentid[i]);
                    Student stutem = studentMapper.selectStudentById(studentid[i]);
                    Student stu = new Student();
                    stu.setFid(studentid[i]);
                    if (studentState != null) {
                        stu.setCfstudentstate(studentState);
                    }
                    stu.setCfkmstlrq(TimeFormat.getNowTimeAsMiao());
                    if (cftlyy.length != 0) {
                        //科目三退练原因
                        stu.setCfkmstlyy(cftlyy[i]);
                    }
                    //科目三退练次数
                    stu.setCfkmstlcs((long) (Integer.parseInt(String.valueOf(stutem.getCfbimudf0060() == null ? 0 : stutem.getCfbimudf0060())) + 1));
                    studentMapper.updateByPrimaryKeySelective(stu);
                }

                //获取操作人信息
                User user = (User) session.getAttribute("user");

                //获取当前修改时间
                String createTime = TimeFormat.timeFormat(new Date());
                //修改时间
                dropexercisethree.setFlastupdatetime(createTime);
                //修改人id
                dropexercisethree.setFauditorid(user.getFid());

                dropexercisethree.setCfbillstate(Long.valueOf("5"));
                dropexercisethreeMapper.updateByPrimaryKeySelective(dropexercisethree);
                addLog(dropexercisethree.getFid(), "2");
            }
            //如果是不同意状态则修改单据状态为保存
            if ("2".equals(agreed)) {
                dropexercisethree.setCfbillstate(Long.valueOf("3"));
                dropexercisethreeMapper.updateByPrimaryKeySelective(dropexercisethree);
            }
            return serverResponse;
        }
        if ("3".equalsIgnoreCase(state)) {
            dropexercisethree.setCfbillstate(Long.valueOf("3"));
            dropexercisethree.setFsourcefunction(auditcomment);
            axamine.againstAuditProcess(dropexercisethree.getFnumber(), auditcomment);
            dropexercisethreeMapper.updateByPrimaryKeySelective(dropexercisethree);
            addLog(dropexercisethree.getFid(), "3");

            return ServerResponse.createBySuccessMessage("反审核成功！");
        }

        return ServerResponse.createByError();
    }

    public void addLog(String fid, String state) {

        List<Dropexercisethreeentry> dropexercisetwoentryList = dropexercisethreeentryMapper.selectDropexercisethreeentyyByFparentidAsthree(fid);

        for (Dropexercisethreeentry dropexercisethreeentry : dropexercisetwoentryList) {

            String cftlyy = dropexercisethreeentry.getCftlyy() == null ? "" : String.valueOf(dropexercisethreeentry.getCftlyy());

            if ("1".equals(state)) {
                customLogService.addLog("科目三退练进行提交", "提交了学员:" +
                                dropexercisethreeentry.getCfstuno() + dropexercisethreeentry.getCfstudentName() + "科目二退练!退练教练:" +
                                dropexercisethreeentry.getCfcoachName() + "退练原因:" + cftlyy
                                + "单据编号：" + dropexercisethreeentry.getFnumber(),
                        dropexercisethreeentry.getCfstudentid(), ((User) session.getAttribute("user")).getFid());
            }
            if ("2".equals(state)) {
                customLogService.addLog("科目三退练进行审核", "审核了学员:" +
                                dropexercisethreeentry.getCfstuno() + dropexercisethreeentry.getCfstudentName() + "科目二退练!退练教练:" +
                                dropexercisethreeentry.getCfcoachName() + "退练原因:" + cftlyy
                                + "单据编号：" + dropexercisethreeentry.getFnumber(),
                        dropexercisethreeentry.getCfstudentid(), ((User) session.getAttribute("user")).getFid());
            }
            if ("3".equals(state)) {
                customLogService.addLog("科目三退练进行反审核", "反审核了学员:" +
                                dropexercisethreeentry.getCfstuno() + dropexercisethreeentry.getCfstudentName() + "科目二退练!退练教练:" +
                                dropexercisethreeentry.getCfcoachName() + "退练原因:" + cftlyy
                                + "单据编号：" + dropexercisethreeentry.getFnumber(),
                        dropexercisethreeentry.getCfstudentid(), ((User) session.getAttribute("user")).getFid());
            }
        }
    }


    //查询子类信息
    @Override
    public List<Dropexercisethreeentry> selectDropexercisethreeentryByFid(String fid, String cfstudentname) {
        return dropexercisethreeentryMapper.selectDropexercisethreeentryByFparentid(fid, cfstudentname);
    }
}
